package com.css.dzj.dzjap.common.menu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.dzj.dzjap.common.menu.vo.DirctVO;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.common.util.Restful.RestfulUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dict")
@PropertySource(value = "classpath:base.properties")
public class GetDictController {
    @Autowired
    private HttpServletRequest request;
    @Value("${BASE_URL}")
    private String BASE_URL;

    /**
     * 获取字典项
     *
     * @param category
     * @return
     */
    @PostMapping("/getdict/{category}")
    public R getDict(@PathVariable String category) {
        if("d_org".equals(category)){
            List<DirctVO> results= new ArrayList<>();
            //获取中国地震局的组织机构信息
            String deptId = "root";
            String url = BASE_URL + "/platform/api/org/depts/" + deptId + "/subdepts";
            HttpMethod method = HttpMethod.GET;
            Map map = new HashMap();
            List list = (List) RestfulUtil.sendRestful(this.request, map, url, method).get("data");
            String supdeptId = "";
            for( int i=0;i<list.size();i++){
                if("中国地震局".equals(((HashMap)list.get(i)).get("deptName").toString())){
                    DirctVO   dirctVO = new DirctVO();
                    dirctVO.setDictName(((HashMap)list.get(i)).get("deptName").toString());
                    dirctVO.setDictCode(((HashMap)list.get(i)).get("deptCode").toString());
                    supdeptId = (((HashMap)list.get(i)).get("deptId").toString());
                    results.add(dirctVO);
                }
            }
            //获取中国地震下级
            String ur2 = BASE_URL + "/platform/api/org/depts/" + supdeptId + "/subdepts";
            HttpMethod methods = HttpMethod.GET;
            Map maps = new HashMap();
            List body = (List) RestfulUtil.sendRestful(this.request, maps, ur2, methods).get("data");
            for( int i=0;i<body.size();i++){
                DirctVO   dirctVO = new DirctVO();
                dirctVO.setDictName(((HashMap)body.get(i)).get("deptName").toString());
                dirctVO.setDictCode(((HashMap)body.get(i)).get("deptCode").toString());
                results.add(dirctVO);
            }
            Map<String, Object > map1 = new HashMap<>();
            map1.put("result",results);
            String rez = JSON.toJSONString(map1);
            JSONObject jsonObject  =JSONObject.parseObject(rez);
            return R.ok().put("data", jsonObject);
        }else{
            String url = BASE_URL + "/platform/api/sys/dicts/dictitems/dictcode/" + category;
            Map<String, String> map = new HashMap<>();
            HttpMethod method = HttpMethod.GET;
            List result1 = (List) RestfulUtil.sendRestful(request, map, url, method).get("data");
            List<DirctVO> results= new ArrayList<>();
            for( int i=0;i<result1.size();i++){
                DirctVO   dirctVO = new DirctVO();
                dirctVO.setDictName(((HashMap)result1.get(i)).get("dictItemName").toString());
                dirctVO.setDictCode(((HashMap)result1.get(i)).get("dictItemCode").toString());
                results.add(dirctVO);
            }
            Map<String, Object > map1 = new HashMap<>();
            map1.put("result",results);
            String rez = JSON.toJSONString(map1);
            JSONObject jsonObject  =JSONObject.parseObject(rez);
            return R.ok().put("data", jsonObject);
        }

    }
}
